﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using DataCom;
using XmlDataCommunication.Properties;
using LineWatch.Common;

namespace XmlDataCommunication
{
    public class XmlUserDataAccess : IUserAccess
    {
        public bool Login(string userName, string password, ref bool canSetShift, ref bool hasRight)
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNode node = doc.SelectSingleNode("//YHXX/Record[UserName='" + userName + "' and Password='" + password + "']");
            if (node != null)
            {
                string right = node.SelectSingleNode("./Right").InnerText;
                string lineID = node.SelectSingleNode("./Line_ID").InnerText;
                if (right == "1") hasRight = true;
                else hasRight = false;
                if (right == "1" && lineID == "O") canSetShift = true;
                else canSetShift = false;
                return true;
            }
            else
            {
                canSetShift = false;
                hasRight = false;
                return false;
            }
        }


        public UserAndLineCodeModel GetUserAndLineCode(string userName)
        {
            UserAndLineCodeModel model = new UserAndLineCodeModel();

            XmlDocument doc = GeneralFunctions.GetXmlDocument();
            XmlNodeList nodes = doc.SelectNodes("//YHXX/Record[UserName='" + userName + "']");
            if (nodes != null)
            {
                model.UserAndLineCodeTable = GeneralFunctions.GetDataTableByXmlNodes(nodes);
            }

            return model;
        }
    }
}
